﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.BBS.Entity;
using PowerCreator.LMS.Web.BBS;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_BBSManager_ManagerTitleInfo : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerBBSTitleInfo objManagerBBSTitleInfo = new ManagerBBSTitleInfo();
    //ManagerBBSTitleType objManagerBBSTitleType = new ManagerBBSTitleType();

    ManagerBBS objManagerBBS = new ManagerBBS();
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.ManagerTitleInfo))
        //{
        //    Script.ShowAndHistory("您没有管理帖子的权限!", -1);
        //    return;
        //}
        if (!IsPostBack)
        {

            ViewState["SortOrder"] = "ThemeID";
            ViewState["OrderDire"] = "Desc";
            BandDomainList();
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);

            InitPage();
        }
    }

    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_ManagerTitleInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_seach.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        

        base.InitPage();
    }

    private void BandDomainList()
    {

                string search = "";
        DataTable dt = objManagerBBS.QueryDomain(search);
        ArrayList list = new ArrayList();
        list = GetDepartmentTree(dt);
        //ListItem li = new ListItem();
        //li.Text = "顶级版块";
        //li.Value = "0";
        //list.Insert(0, li);

        ddl_Domain.DataSource = list;
        ddl_Domain.DataTextField = "Text";
        ddl_Domain.DataValueField = "Value";
        ddl_Domain.DataBind();
        ddl_Domain.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
    }

       /// <summary>
    /// 获取树结构类型的部门信息，List中为Text和Value值，ListItem项
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public ArrayList GetDepartmentTree(DataTable dt)
    {
        ArrayList list = new ArrayList();
        InitTree(null, dt, "0", ref list);
        return list;
    }

    /// <summary>
    /// 递归初始化树结构，在ArrayList方式显示
    /// </summary>
    /// <param name="pNode">TreeNode节点</param>
    /// <param name="dt">部门数据</param>
    /// <param name="parentid">上一级部门ID</param>
    /// <param name="listt"></param>
    private void InitTree(TreeNode pNode, DataTable dt, string parentid, ref ArrayList listt)
    {
        if (dt.Rows.Count > 0)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[DomainFatherID]=" + parentid;
            foreach (DataRowView drv in dv)
            {
                TreeNode tempNode = new TreeNode();
                if (pNode == null)
                {
                    //填充根节点
                    tempNode.Text = drv["DomainName"].ToString() + "&nbsp;";
                    tempNode.ShowCheckBox = true;
                    tempNode.Value = drv["DomainID"].ToString();

                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["DomainName"].ToString();
                    li.Value = drv["DomainID"].ToString();

                    listt.Add(li);

                    InitTree(tempNode, dt, drv["DomainID"].ToString(), ref listt);
                }
                else
                {
                    tempNode.Text = drv["DomainName"].ToString();
                    tempNode.Value = drv["DomainID"].ToString();
                    pNode.ChildNodes.Add(tempNode);
                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["DomainName"].ToString();
                    li.Value = drv["DomainID"].ToString();

                    listt.Add(li);

                    InitTree(tempNode, dt, drv["DomainID"].ToString(), ref listt);
                }
            }
        }
    }

    private string GetSearch()
    {
        StringBuilder search = new StringBuilder();
        if (ddl_Domain.SelectedValue != "-1")
        {
            search.Append(" ThemeDomainID	=" + ddl_Domain.SelectedValue);
        }
        if (!string.IsNullOrEmpty(txt_TitleName.Text.Trim()))
        {
            if (search.Length > 0)
            {
                search.Append(" and ThemeTitle like '%" + txt_TitleName.Text + "%'");
            }
            else
            {
                search.Append(" ThemeTitle like '%" + txt_TitleName.Text + "%'");
            }
        }

        //string search = string.Format(" ThemeTitle	 like '%{0}%'", txt_TitleName.Text.Trim());
        return search.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objManagerBBS.CountTheme(search);
        AspNetPager1.PageSize = PowerCreator.LMS.Web.Config.WebConfig.PageSize;

        PreAspNetPager(AspNetPager1);
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        string orderby = "ThemeCreatetime desc";
        dt = objManagerBBS.QueryTheme(search,orderby, PowerCreator.LMS.Web.Config.WebConfig.PageSize, pageIndex);
        DataView view = dt.DefaultView;
        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        view.Sort = sort;

        GridView1.Columns[0].HeaderText = GetLanageValueByKey("Msg_AddQuestionInfo_checkmsg4", PowerCreator.LMS.Language.LanagetItemType.Page);
        GridView1.Columns[1].HeaderText = GetLanageValueByKey("lab_AddRefer_ReferTitle", PowerCreator.LMS.Language.LanagetItemType.WebPage);

        GridView1.Columns[2].HeaderText = GetLanageValueByKey("lab_ManagerTitleInfo_ClickCount", PowerCreator.LMS.Language.LanagetItemType.Page);
        GridView1.Columns[3].HeaderText = GetLanageValueByKey("lbl_EditResourceInfo_AuthorName", PowerCreator.LMS.Language.LanagetItemType.Page);
        GridView1.Columns[4].HeaderText = GetLanageValueByKey("lab_ManagerTitleInfo_CreateTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        //e.Row.Cells[5].Text = GetLanageValueByKey("lab_ManagerTitleInfo_LastRevertUser", PowerCreator.LMS.Language.LanagetItemType.Page);
        GridView1.Columns[6].HeaderText = GetLanageValueByKey("lab_ManagerTitleInfo_LastRevertUser", PowerCreator.LMS.Language.LanagetItemType.Page);
        GridView1.Columns[7].HeaderText = GetLanageValueByKey("lab_ManagerTitleInfo_LastRevertTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        GridView1.Columns[8].HeaderText = GetLanageValueByKey("lab_ManagerTitleInfo_ThemeReversionNum", PowerCreator.LMS.Language.LanagetItemType.Page);

        GridView1.Columns[9].HeaderText = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);
        //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        GridView1.DataSource = dt;
        GridView1.DataBind();


       
     
    }

    protected void btn_seach_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btn_del")
        {
            if(objManagerBBS.DeleteTheme(int.Parse(e.CommandArgument.ToString())))
            {
                Script.Alert(Page, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
            else
            {
                Script.Alert(Page, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
            
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, AspNetPager1.CurrentPageIndex);
        }
        if (e.CommandName == "btn_Edit")
        {
            string url = "EditTitleInfo.aspx?ThemeID=" + Base64.Encode(e.CommandArgument.ToString());
            Script.RedirectURL(url);
        }
        if (e.CommandName == "btn_shenhe")
        {
            //BBSTitleInfo objBBSTitleInfo = new BBSTitleInfo();
            VOTheme objVOTheme = new VOTheme();
            objVOTheme = objManagerBBS.LoadTheme(int.Parse(e.CommandArgument.ToString()));
            if (objVOTheme.ThemeIsVisible == 1)
            {
                objVOTheme.ThemeIsVisible = 0;

            }
            else
            {
                objVOTheme.ThemeIsVisible = 1;
            }

            if (objManagerBBS.UpdateTheme(objVOTheme))
            {
                Script.Alert(Page, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
            else
            {
                Script.Alert(Page, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
            //Script.Alert(this,"msg",objManagerBBSTitleInfo.Update(objBBSTitleInfo));
            string search = GetSearch();
            BinderGV(search, AspNetPager1.CurrentPageIndex);

        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //    e.Row.Cells[0].Text = GetLanageValueByKey("Msg_AddQuestionInfo_checkmsg4", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[1].Text = GetLanageValueByKey("lab_AddRefer_ReferTitle", PowerCreator.LMS.Language.LanagetItemType.WebPage);

        //    e.Row.Cells[2].Text = GetLanageValueByKey("lab_ManagerTitleInfo_ClickCount", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[3].Text = GetLanageValueByKey("lbl_EditResourceInfo_AuthorName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[4].Text = GetLanageValueByKey("lab_ManagerTitleInfo_CreateTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[5].Text = GetLanageValueByKey("lab_ManagerTitleInfo_LastRevertUser", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[6].Text = GetLanageValueByKey("lab_ManagerTitleInfo_LastRevertUser", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[7].Text = GetLanageValueByKey("lab_ManagerTitleInfo_LastRevertTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[8].Text = GetLanageValueByKey("lab_ManagerTitleInfo_ThemeReversionNum", PowerCreator.LMS.Language.LanagetItemType.Page);

        //    e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        //}
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");
            LinkButton btn_shenhe = (LinkButton)e.Row.Cells[6].FindControl("btn_shenhe");
            LinkButton btn_Edit = (LinkButton)e.Row.Cells[6].FindControl("btn_Edit");
            LinkButton btn_del = (LinkButton)e.Row.Cells[6].FindControl("btn_del");

            btn_Edit.Text = GetLanageValueByKey("btn_ManagerQuestion_Edit", PowerCreator.LMS.Language.LanagetItemType.Page);
            btn_del.Text = GetLanageValueByKey("btn_ManagerQuestion_Delete", PowerCreator.LMS.Language.LanagetItemType.Page);
            btn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");

            if (e.Row.Cells[5].Text.Trim() == "0")
            {
                e.Row.Cells[5].Text = GetLanageValueByKey("lab_ManagerTitleInfo_NoView", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn_shenhe.Text = GetLanageValueByKey("lab_ManagerTitleInfo_View", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            else
            {
                e.Row.Cells[5].Text = GetLanageValueByKey("lab_ManagerTitleInfo_View", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn_shenhe.Text = GetLanageValueByKey("lab_ManagerTitleInfo_NoView", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }

    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        //string sPage = e.SortExpression;
        //if (ViewState["SortOrder"].ToString() == sPage)
        //{
        if (ViewState["OrderDire"].ToString() == "Desc")
            ViewState["OrderDire"] = "ASC";
        else
            ViewState["OrderDire"] = "Desc";
        //}
        //else
        //{
        ViewState["SortOrder"] = e.SortExpression;
        //}

        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
}
